package vistop.datatypes;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class FreeAbelianGroup<T> {
	
	public FreeAbelianGroup() {
	}
	
	public Map<T,Integer> add(Map<T,Integer> a, Map<T,Integer> b) {
		Map<T,Integer> sum = new HashMap<T,Integer>();
		for (Entry<T,Integer> e : a.entrySet()) {
			int v = a.get(e.getKey());
			if (b.containsKey(e.getKey())) {
				v += b.get(e.getKey());
				b.remove(e.getKey());
			}
			sum.put(e.getKey(), v);
		}
		if (!b.isEmpty()) {
			sum.putAll(b);
		}
		return sum;
	}
	
	public Map<T,Integer> multiply(int a, Map<T,Integer> b) {
		Map<T,Integer> result = new HashMap<T,Integer>();
		for (Entry<T,Integer> e : b.entrySet()) {
			result.put(e.getKey(), e.getValue()*a);
		}
		return result;
	}
}
